# 用户取款界面
from PyQt5 import QtWidgets, QtGui
from mysql import select_sql, insert_sql, updata_sql
import temp


class user_qukuan(QtWidgets.QMainWindow):


    def __init__(self):
        super(user_qukuan, self).__init__()
        self.card = temp.card

    def setupUI(self):
        self.setWindowTitle("卡号：" + self.card + "取款")
        self.setGeometry(100, 100, 300, 200)
        self.Qlable1()
        self.Qlineedit()
        self.Qbutton1()
        self.Qlable2()
        self.Qbutton2()

    def Qlable1(self):
        qlable = QtWidgets.QLabel(self)
        qlable.setText("请输入取款金额：")
        qlable.setGeometry(30, 30, 100, 30)

    def Qlineedit(self):
        self.qlineedit = QtWidgets.QLineEdit(self)
        self.qlineedit.setGeometry(120, 30, 140, 30)
        self.qlineedit.setValidator(QtGui.QIntValidator(self))

    def Qlable2(self):
        qlable = QtWidgets.QLabel(self)
        qlable.setText("提示：输入金额为100的整数，且单次不得超过5000元")
        qlable.setGeometry(20, 80, 270, 30)

    def Qbutton1(self):
        qbutton = QtWidgets.QPushButton(self)
        qbutton.setText("取款")
        qbutton.setGeometry(30, 140, 50, 30)
        qbutton.clicked.connect(self.Qbutton1_click)

    # 输入内容的验证
    # 先验证是否是100的倍数
    def clean_100(self):
        self.money = int(self.qlineedit.text())
        if self.money % 100 != 0:
            return 0
        return 1

    # 验证输入的内容是否大于5000或者大于余额或者是负数
    def clean_size(self):
        sql = "SELECT * FROM ATM.User where card = '{0}' ".format(self.card)
        result = select_sql(sql)
        self.money2 = result['money']
        if self.money < 0 or self.money > 5000 or self.money > self.money2:
            return 0
        return 1

    def Qbutton1_click(self):
        clean1 = self.clean_100()
        if clean1 == 0:
            self.msg = QtWidgets.QMessageBox(QtWidgets.QMessageBox.Warning, "警告", "输入的内容不是100的整数")
            return self.msg.show()
        else:
            clean2 = self.clean_size()
            if clean2 == 0:
                self.msg = QtWidgets.QMessageBox(QtWidgets.QMessageBox.Warning, "警告",
                                                 "输入的内容大于余额或大于5000或为负数")
                return self.msg.show()
            else:
                # 取款
                # 记录与更新余额
                from datetime import datetime
                cdate = datetime.today()
                sql = "INSERT INTO ATM.recode(card,cdate,money,status,card2) VALUES ('{0}'," \
                      "'{1}','{2}','取款',0)".format(self.card, cdate, self.money)
                insert_sql(sql)
                money = self.money2 - self.money
                sql = "UPDATE ATM.User SET ATM.User.money = '{0}' where card = '{1}'".format(money, self.card)
                updata_sql(sql)
                # 成功后回到主窗口
                self.msg = QtWidgets.QMessageBox(QtWidgets.QMessageBox.Information, "提示",
                                                 "取款成功")
                return self.msg.show()

    def Qbutton2(self):
        qbutton = QtWidgets.QPushButton(self)
        qbutton.setText("返回")
        qbutton.setGeometry(100, 140, 50, 30)
        qbutton.clicked.connect(self.Qbutton2_click)

    def Qbutton2_click(self):
        self.close()
        # 返回后，主窗口不解除限制
        from user_main import user_main
        self.a = user_main()
        self.a.setupUI()
        self.a.show()
